package com.example.administrator.jingpingou.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.application.MyApplication;
import com.example.administrator.jingpingou.bean.LoginUserBean;
import com.example.administrator.jingpingou.login.LoginActivity;

import org.w3c.dom.NameList;
import org.xutils.DbManager;
import org.xutils.db.Selector;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

public class RegisterActivity extends AppCompatActivity {

    private DbManager mDb;
    private ImageView mImageBack;
    private TextView mTextLog, mTextRegister;
    private EditText mEditName, mEditPwd, mPwdAgain;
    private String name;
    private String pwd;
    private String pwdAgain;
    private MyApplication application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        DbManager.DaoConfig config = new DbManager.DaoConfig();
        config.setDbName("UserLogin");
        mDb = x.getDb(config);

        initView();
        setListener();
    }

    private void initData() {
        name = mEditName.getText().toString();
        pwd = mEditPwd.getText().toString();
        pwdAgain = mPwdAgain.getText().toString();
    }


    private void setListener() {
        mImageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTextLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
            }
        });
        mTextRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();

                    try {
                        List<LoginUserBean> nameList = mDb.selector(LoginUserBean.class).where("Name", "=", name).findAll();
                        if (nameList!=null){
                        if (nameList.size() >= 1) {
                            Toast.makeText(RegisterActivity.this, "用户名已存在,请重新输入", Toast.LENGTH_SHORT).show();
                        } else {
                            application = (MyApplication) getApplication();
                            if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd) && pwd.equals(pwdAgain)) {
                                LoginUserBean user = new LoginUserBean();
                                user.setName(name);
                                user.setPassword(pwd);
                                application.setUserBeen(user);
                                try {
                                    mDb.save(user);
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                            }
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                        }else {
                            application = (MyApplication) getApplication();
                            if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd) && pwd.equals(pwdAgain)) {
                                LoginUserBean user = new LoginUserBean();
                                user.setName(name);
                                user.setPassword(pwd);
                                application.setUserBeen(user);
                                try {
                                    mDb.save(user);
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                            }
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    } catch (DbException e) {
                        e.printStackTrace();
                    }

            }
        });
    }

    private void initView() {
//        mImageBack= (ImageView) findViewById(R.id.iv);
        mEditName = (EditText) findViewById(R.id.et_activity_register_name);
        mEditPwd = (EditText) findViewById(R.id.et_activity_register_pwd);
        mPwdAgain = (EditText) findViewById(R.id.et_activity_register_pwd_again);
        mTextLog = (TextView) findViewById(R.id.tv_activity_register_log);
        mTextRegister = (TextView) findViewById(R.id.tv_activity_register_register);
        mImageBack = (ImageView) findViewById(R.id.iv_activity_regster_back);
    }


}
